import { defineStore } from 'pinia'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import type { Tool } from '@/types'

export const useToolStore = defineStore('tool', {
  state: () => ({
    tools: [
      {
        id: 'json-formatter',
        name: 'JSON 格式化',
        description: '格式化和验证 JSON 数据',
        icon: ElementPlusIconsVue.Document,
        path: '/tools/json',
        category: 'formatter'
      },
      {
        id: 'xml-formatter',
        name: 'XML 格式化',
        description: '格式化和验证 XML 数据',
        icon: ElementPlusIconsVue.Edit,
        path: '/tools/xml',
        category: 'formatter'
      },
      {
        id: 'id-generator',
        name: '身份证号生成',
        description: '生成随机身份证号码',
        icon: ElementPlusIconsVue.User,
        path: '/tools/id-generator',
        category: 'generator'
      },
      {
        id: 'calendar',
        name: '日历待办',
        description: '按周、日、月查看和管理待办事项',
        icon: ElementPlusIconsVue.Calendar,
        path: '/tools/calendar',
        category: 'tools'
      }
    ] as Tool[],
    searchQuery: ''
  }),
  getters: {
    filteredTools: (state) => {
      const query = state.searchQuery.toLowerCase()
      return state.tools.filter(tool => 
        tool.name.toLowerCase().includes(query) || 
        tool.description.toLowerCase().includes(query)
      )
    }
  },
  actions: {
    setSearchQuery(query: string) {
      this.searchQuery = query
    },

    addTool(tool: Tool) {
      this.tools.push(tool)
      // 可以在这里添加持久化存储
      localStorage.setItem('custom-tools', JSON.stringify(this.tools))
    },

    deleteTool(id: string) {
      const index = this.tools.findIndex(tool => tool.id === id)
      if (index !== -1) {
        this.tools.splice(index, 1)
        // 更新持久化存储
        localStorage.setItem('custom-tools', JSON.stringify(this.tools))
      }
    },

    // 初始化时加载自定义工具
    loadCustomTools() {
      const customTools = localStorage.getItem('custom-tools')
      if (customTools) {
        this.tools = JSON.parse(customTools)
      }
    }
  }
}) 